package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.OrganizationDao;
import com.zja.model.Organization;

import java.util.List;

/**
 * @author JA
 */
public class OrganizationDaoImpl extends BaseDAO implements OrganizationDao {

    @Override
    public List<Organization> selectAll() {
        String sql = "SELECT * FROM organization";
        try {
            return executeQuery(Organization.class, sql, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Organization selectById(Long id) {
        String sql = "SELECT * FROM organization WHERE id = ?";
        try {
            return executeQueryBean(Organization.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(Organization organization) {
        String sql = "INSERT INTO organization(name, creator_id, create_time, update_time) VALUES(?, ?, ?, ?)";
        try {
            return executeUpdate(sql, organization.getName(), organization.getCreatorId(), organization.getCreateTime(), organization.getUpdateTime());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(Organization organization) {
        String sql = "UPDATE organization SET name = ?, creator_id = ?,  update_time = ? WHERE id = ?";
        try {
            return executeUpdate(sql, organization.getName(), organization.getCreatorId(),  organization.getUpdateTime(), organization.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        String sql = "DELETE FROM organization WHERE id = ?";
        try {
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
